package pers.ascend.spring.cloud.dalston.service.governance.eureka.consumer.ribbon.hystrix.manage.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import pers.ascend.spring.cloud.dalston.service.governance.eureka.consumer.ribbon.hystrix.manage.service.ConsumerService;

import java.util.concurrent.TimeUnit;

/**
 * 说明：
 *
 * @author ascend
 * 2021/3/9 10:25
 */
@RestController
@RequestMapping(value = "/consumer")
@Validated
public class EurekaConsumerRibbonHystrixController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ConsumerService service;

    @HystrixCommand(fallbackMethod = "helloFallBack")
    @GetMapping(value = "/hello")
    public String hello(@RequestParam("name") @NotBlank String name) {
        if (log.isInfoEnabled()) {
            log.info("ConsumerController.hello()");
        }
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return service.hello(name);
    }

    public String helloFallBack(String name) {
        return "hello fail '" + name + "'";
    }

}
